using System.Collections.Generic;
using UnityEngine;

public static partial class MapHandler
{
	public static void RegisteNetEventHandlers()
	{
		MessageManager.RegistMessage(80001,typeof(Map.ReqLoginMessage));
		MessageManager.RegistMessage(80002,typeof(Map.ResLoginMessage));
		MessageManager.RegistNetEventHandler(80002, OnRecvResLoginMessage);
		MessageManager.RegistMessage(80003,typeof(Map.ReqCreateRoomMessage));
		MessageManager.RegistMessage(80004,typeof(Map.ResCreateRoomMessage));
		MessageManager.RegistNetEventHandler(80004, OnRecvResCreateRoomMessage);
		MessageManager.RegistMessage(80005,typeof(Map.ReqCreateRoleMessage));
		MessageManager.RegistMessage(80006,typeof(Map.ResCreateRoleMessage));
		MessageManager.RegistNetEventHandler(80006, OnRecvResCreateRoleMessage);
		MessageManager.RegistMessage(80010,typeof(Map.ReqJoinRoomMapMessage));
		MessageManager.RegistMessage(80011,typeof(Map.ResJoinRoomMapMessage));
		MessageManager.RegistNetEventHandler(80011, OnRecvResJoinRoomMapMessage);
		MessageManager.RegistMessage(80013,typeof(Map.ReqLeaveRoomMapMessage));
		MessageManager.RegistMessage(80014,typeof(Map.ResLeaveRoomMapMessage));
		MessageManager.RegistNetEventHandler(80014, OnRecvResLeaveRoomMapMessage);
		MessageManager.RegistMessage(80015,typeof(Map.ResRoomInfoMessage));
		MessageManager.RegistNetEventHandler(80015, OnRecvResRoomInfoMessage);
		MessageManager.RegistMessage(80016,typeof(Map.ReqRoomBattleBeReadyMessage));
		MessageManager.RegistMessage(80017,typeof(Map.ResBattleStartMessage));
		MessageManager.RegistNetEventHandler(80017, OnRecvResBattleStartMessage);
		MessageManager.RegistMessage(80018,typeof(Map.ReqLockStepCommandMessage));
		MessageManager.RegistMessage(80019,typeof(Map.ResLockStepCommandMessage));
		MessageManager.RegistNetEventHandler(80019, OnRecvResLockStepCommandMessage);
		MessageManager.RegistMessage(80020,typeof(Map.ReqBattleEndMessage));
		MessageManager.RegistMessage(80021,typeof(Map.ResBattleEndMessage));
		MessageManager.RegistNetEventHandler(80021, OnRecvResBattleEndMessage);
		MessageManager.RegistMessage(80050,typeof(Map.ResRoomListInfoMessage));
		MessageManager.RegistNetEventHandler(80050, OnRecvResRoomListInfoMessage);
		MessageManager.RegistMessage(80051,typeof(Map.ResRoomAddMessage));
		MessageManager.RegistNetEventHandler(80051, OnRecvResRoomAddMessage);
		MessageManager.RegistMessage(80052,typeof(Map.ResRoomRemovedMessage));
		MessageManager.RegistNetEventHandler(80052, OnRecvResRoomRemovedMessage);
		MessageManager.RegistMessage(80053,typeof(Map.ResRoomUpdatedMessage));
		MessageManager.RegistNetEventHandler(80053, OnRecvResRoomUpdatedMessage);
	}
	static void OnRecvResLoginMessage(Google.Protobuf.IMessage message)
	{
		if(message is Map.ResLoginMessage recvedMsg)
		{
			if(recvedMsg.State != 0)
			{
                Debug.Log($"[client]:Login Failed state = {recvedMsg.State}!!!");
                return;
			}
			Debug.Log($"[client]:Login Succeed:[PlayerId:{recvedMsg.PlayerId}][RoleId:{recvedMsg.RoleId}]!");
            MainPlayer.MainPlayerId = recvedMsg.PlayerId;
			MainPlayer.RoleId = recvedMsg.RoleId;
			MainPlayer.PlayerName = recvedMsg.PlayerName;
            EventDispatchManager.Instance.SendEvent(EventId.Game.EVENT_PLAYER_LOGINED);
		}
	}
	static void OnRecvResCreateRoomMessage(Google.Protobuf.IMessage message)
	{
		if(message is Map.ResCreateRoomMessage recvedMsg)
		{
			if(recvedMsg.RoomId < 0)
            {
				Debug.LogError("[client]:Create Room Failed");
				EventDispatchManager.Instance.SendEvent(EventId.Game.EVENT_ROOM_CREATED_FAILED);
				MessageBoxQueueManager.Instance.PopupMessage(recvedMsg.RoomId);
				return;
            }

			Debug.Log($"[client]:Create Room Succeed:[RoomId:{recvedMsg.RoomId}]!");
			EventDispatchManager.Instance.SendEvent(EventId.Game.EVENT_ROOM_CREATED,new EventArgv<int, string, string>
			{
				a = recvedMsg.RoomId,
				b = recvedMsg.RoomName,
				c = recvedMsg.RoomPassword,
			});
		}
	}
	static void OnRecvResCreateRoleMessage(Google.Protobuf.IMessage message)
	{
		if(message is Map.ResCreateRoleMessage recvedMsg)
		{
		}
	}
	static void OnRecvResJoinRoomMapMessage(Google.Protobuf.IMessage message)
	{
		if(message is Map.ResJoinRoomMapMessage recvedMsg)
		{
		}
	}
	static void OnRecvResLeaveRoomMapMessage(Google.Protobuf.IMessage message)
	{
		if(message is Map.ResLeaveRoomMapMessage recvedMsg)
		{
		}
	}
	static void OnRecvResRoomInfoMessage(Google.Protobuf.IMessage message)
	{
		if(message is Map.ResRoomInfoMessage recvedMsg)
		{
		}
	}
	static void OnRecvResBattleStartMessage(Google.Protobuf.IMessage message)
	{
		Debug.Log("[Client]:OnRecvResBattleStartMessage");
		if(message is Map.ResBattleStartMessage recvedMsg)
		{
		}
	}
	static void OnRecvResLockStepCommandMessage(Google.Protobuf.IMessage message)
	{
		if(message is Map.ResLockStepCommandMessage recvedMsg)
		{
		}
	}
	static void OnRecvResBattleEndMessage(Google.Protobuf.IMessage message)
	{
		if(message is Map.ResBattleEndMessage recvedMsg)
		{
		}
	}
	static void OnRecvResRoomListInfoMessage(Google.Protobuf.IMessage message)
	{
		Debug.Log("[OnRecvResRoomListInfoMessage]");
		if(message is Map.ResRoomListInfoMessage recvedMsg)
		{
			Debug.Log("[OnRecvResRoomListInfoMessage] recvedMsg ===");
			if(null == RoomManager.Instance)
            {
				Debug.LogError("RoomManager Is Null");
				return;
            }
			RoomManager.Instance.Initialze(recvedMsg.RoomInfos);
        }
	}
	static void OnRecvResRoomAddMessage(Google.Protobuf.IMessage message)
	{
		if(message is Map.ResRoomAddMessage recvedMsg)
		{
			RoomManager.Instance.Add(recvedMsg.RoomInfo);
        }
	}
	static void OnRecvResRoomRemovedMessage(Google.Protobuf.IMessage message)
	{
		if(message is Map.ResRoomRemovedMessage recvedMsg)
		{
            RoomManager.Instance.Remove(recvedMsg.RoomId);
        }
	}
	static void OnRecvResRoomUpdatedMessage(Google.Protobuf.IMessage message)
	{
		if(message is Map.ResRoomUpdatedMessage recvedMsg)
		{
            RoomManager.Instance.Modify(recvedMsg.RoomInfo);
        }
	}
}
